<template>
  <div>
    <el-main class="main">
      <div>
        <el-row>
          <el-col :span="6">
            <div class="env-appraise">
              <div class="title">健康光环境评价</div>
              <div class="content">
                <div class="text">
                  <span v-if="evaluateObj.comment&&evaluateObj.comment.length>0">
                    <span v-for="(item,index) in evaluateObj.comment"
                          :key="index">{{item}}</span>
                  </span>
                  <span v-else>暂无评价</span>
                </div>
                <div>
                  <div class="evaluate-text">{{evaluateObj.evaluate|evaluateFil()}}</div>
                </div>
              </div>
            </div>
            <over-view style="margin-top:15px;"
                       :deviceId="deviceId"></over-view>
          </el-col>
          <el-col :span="18"
                  style="padding-left:15px">
            <map-info :deviceId="deviceId"
                      @selectLocationIdFun="selectLocationIdFun"
                      :allDeviceData="allDeviceData"
                      :platformId="platformId"></map-info>
          </el-col>
        </el-row>
        <el-row style="margin-top:15px">
          <el-col :span="8">
            <div class="env-appraise line-color-box">
              <div class="title">
                光谱分布
              </div>
              <line-chart-color ref="chart2_2"
                                :chart-data="relativeSpectrum"
                                color="#36D3EC"
                                head-title=""
                                height="239px" />
            </div>
            <warning-info style="margin-top:15px;"
                          :alertData="alertData"
                          :platformId="platformId"></warning-info>
          </el-col>
          <el-col :span="11">
            <div class="env-appraise line-color-box"
                 style="margin: 0 15px">
              <el-row type="flex"
                      justify="space-between">
                <div class="title">
                  非视觉指标当日监测达标时长
                </div>
                <div class="right-box">
                  <span class="point"></span><span class="name">监测总时长</span>
                  <span class="point"
                        style="background-color:#4CD2EE"></span><span class="name">EML达标时长</span>
                  <span class="point"
                        style="background-color:#308EF9"></span><span class="name">EDI达标时长</span>
                  <span class="point"
                        style="background-color:#0044AA"></span><span class="name">CS达标时长</span>
                </div>
              </el-row>

              <three-bar-chart :csChartData="csChartData"
                               :emlChartData="emlChartData"
                               :ediChartData="ediChartData"
                               :chart-time="chartTime"
                               height="239px"></three-bar-chart>
            </div>
            <env-compliance style="margin: 15px 15px 0 15px"
                            :platformId="platformId"></env-compliance>
          </el-col>
          <el-col :span="5">
            <risk-statistics :platformId="platformId"></risk-statistics>
            <warning-time :platformId="platformId"
                          style="margin-top:15px"></warning-time>
          </el-col>
        </el-row>
      </div>
    </el-main>
  </div>
</template>

<script>
import MapInfo from "./components/mapInfo/index";
import WarningInfo from "./components/warningInfo";
import OverView from "./components/overView";
import RiskStatistics from "./components/riskStatistics";
import WarningTime from "./components/warningTime";
import LineChartColor from "./components/charts/LineChartColor";
import ThreeBarChart from "./components/charts/ThreeBarChart";
import EnvCompliance from "./components/envCompliance/index";
import { getAllDeviceData, getDeviceEvaluate } from "@/api/platformManage";
import store from "@/store/modules/user";
import { cal_lux, cal_data, attainment_rate } from "@/api/OPT";
import { getDurTime, fromMSToH } from "@/utils/time"
export default {
  name: "warningDevice",
  components: {
    WarningInfo,
    MapInfo,
    OverView,
    LineChartColor,
    ThreeBarChart,
    EnvCompliance,
    RiskStatistics,
    WarningTime
  },
  data() {
    return {
      chart: null,
      platformId: "",
      platformName: "",
      platformLogo: "",
      selectDeviceIndex: 0,
      deviceId: "",
      deviceName: "",
      alertData: [],
      allDeviceData: [],
      onlineData: [],
      // timer: null,
      evaluateObj: {},
      evaluateList: [//左上角单点评价 GREAT 优，GOOD 良，OK 中，BAD 差
        {
          code: 'GREAT',
          lable: '优'
        },
        {
          code: 'GOOD',
          lable: '良'
        },
        {
          code: 'OK',
          lable: '中'
        },
        {
          code: 'BAD',
          lable: '差'
        },
      ],

      relativeSpectrum: [],
      chartTime: [],
      csChartData: [],
      ediChartData: [],
      emlChartData: [],
    };
  },
  filters: {
    evaluateFil(val) {
      switch (val) {
        case 'GREAT':
          return '优';
        case 'GOOD':
          return '良';
        case 'OK':
          return '中';
        case 'BAD':
          return '差';
        default:
          return "NC"
      }
    }
  },
  watch: {
    deviceId() {
      this.getDeviceEvaluateFun();
      this.getColorData("NORMALIZED_SPECTRUM");
      this.getAttainmentRate();
    }
  },
  mounted() {
    if (this.$route.query.platformId) {
      this.platformId = this.$route.query.platformId;
      this.platformName = this.$route.query.name;
      this.platformLogo = this.$route.query.logo;
      this.$store.commit("user/selectProjectMuts", {
        id: this.platformId,
        name: this.platformName,
        logo: this.platformLogo
      });
    } else {
      var selectProject = JSON.parse(localStorage.getItem('selectProject'));
      this.platformId = selectProject.id;
      this.platformName = selectProject.name;
      this.platformLogo = selectProject.logo;
    }
    this.getAllDeviceFun();
  },
  // unmounted() {
  //   clearInterval(this.timer);
  // },
  methods: {
    getColorData(type) {
      /**
       * D_XY 色品坐标
       * OPT_ALL 评价指标
       * NORMALIZED_SPECTRUM 相对光谱
       * R_VECTOR 显色指数
       */
      let params = {
        deviceId: this.deviceId,
        type,
      }
      params.ts = new Date().getTime();
      cal_data(params).then(res => {
        if (type == "D_XY") {
          let data = res.value;
          if (!data) {
            return;
          }
          this.spectogramData = data.map(e => {
            if (!isNaN(e)) {
              return e.toFixed(2);
            } else {
              return 0;
            }
          });
        } else if (type == "NORMALIZED_SPECTRUM") {
          let data = res.value;
          if (!data) {
            return;
          }
          this.relativeSpectrum = data.map(e => {
            if (!isNaN(e)) {
              return e.toFixed(2);
            } else {
              return 0;
            }
          });

        } else if (type == "R_VECTOR") {
          let data = res.value;
          if (!data) {
            return;
          }
          this.colorIndex = data;
          this.colorIndex = data.map(e => {
            if (!isNaN(e)) {
              if (e > -100 && e < 100) {
                return e.toFixed(1);
              }
              return "-";
            }
            return "-";
          });
        } else if (type == "OPT_ALL") {
          this.optAll = res.value;
        }
      })
    },
    getAttainmentRate() {
      attainment_rate(this.deviceId).then(res => {
        let list = res.value;
        this.chartTime = [];
        this.csChartData = [];
        this.ediChartData = [];
        this.emlChartData = [];
        for (let i = 0; i < list.length; i++) {
          const element = list[i];
          let time = getDurTime(element.start, element.end);
          this.chartTime.push(time);
          this.csChartData.push(fromMSToH(element.csMills, 2))
          this.ediChartData.push(fromMSToH(element.ediMills, 2))
          this.emlChartData.push(fromMSToH(element.emlMills, 2))

        }
      })
    },
    getDeviceEvaluateFun() {
      getDeviceEvaluate(this.deviceId).then(res => {

        this.evaluateObj = res.value ? res.value : {};

      })
    },
    getAllDeviceFun() {
      getAllDeviceData(this.platformId).then(res => {
        let data = res.value;
        this.allDeviceData = data;
        this.alertData = data.filter(e => {
          if (e.data != null) {
            return e.alert == true;
          }
        });

        this.onlineData = data.filter(e => {
          if (e.state == "ONLINE") {
            return e;
          }
        });
        this.selectDeviceIndex = 0;
        this.deviceId = this.onlineData[0].deviceId;
        this.deviceName = this.onlineData[0].name;
      });
      // //定时器轮播设备
      // this.timer = setInterval(() => {
      //   if (this.selectDeviceIndex < this.onlineData.length - 2) {
      //     this.selectDeviceIndex++;
      //     this.deviceId = this.onlineData[this.selectDeviceIndex].deviceId;
      //     this.deviceName = this.onlineData[this.selectDeviceIndex].name;
      //   } else {
      //     this.selectDeviceIndex = 0;
      //     this.deviceId = this.onlineData[0].deviceId;
      //     this.deviceName = this.onlineData[0].name;
      //   }
      // }, 1000 * 60);
    },
    selectLocationIdFun(item) {
      if (item.state == "ONLINE") {
        this.deviceId = item.id;
        this.deviceName = item.name;
      }
    }
  }
};
</script>
<style>
body {
  background-color: #edf0f3;
  font-family: Segoe UI;
}
.header {
  height: 59px;
  width: 100%;
  background-color: #2d9dff;
}
</style>
<style lang="scss" scoped>
.main {
  padding: 15px 15px 0 15px !important;
  box-sizing: content-box;
}
.env-appraise {
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  .title {
    font-size: 16px;
    font-weight: bold;
    line-height: 21px;
    color: #0a0a0a;
  }
  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .text {
      font-size: 16px;
      font-weight: 400;
      line-height: 21px;
      color: #535353;
      margin-right: 20px;
    }
  }
  .point {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: rgba(218, 220, 225, 0.39);
    border-radius: 50%;
    opacity: 1;
    margin-right: 3px;
  }
  .name {
    font-size: 12px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 16px;
    color: #535353;
    opacity: 1;
    margin-right: 10px;
  }
}
.line-color-box {
  padding: 13px;
}
.evaluate-text {
  background-image: url("../../../assets/platformDetail/great.png");
  min-width: 150px;
  min-height: 100px;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  text-align: center;
  padding-top: 40px;
  font-weight: bold;
  font-size: 30px;
}
@media screen and (max-width: 1440px) {
  .env-appraise {
    .right-box {
      display: none;
    }
  }
}
</style>
